//
//  WYEmoticonCollectionViewCell.swift
//  WYWeibo
///Users/wangyi/Desktop/Swift/05-iPhone项目-第3天(项目框架搭建)/02-其他资源/素材/Emoticons.bundle
//  Created by wangyi on 16/7/26.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

class WYEmoticonCollectionViewCell: UICollectionViewCell {
    
    //储存20 个表情按钮  j将按钮添加到数组 方便遍历创建
    private lazy var emoticonButtons: [WYEmoticonButton] = [WYEmoticonButton]()
    
    
    //绑定数据模型
    var emoticons: [WYEmoticon]? {
    
        didSet {
        
            guard let ets = emoticons else {
            
                return
            }
            //隐藏全部按钮
            for value in emoticonButtons {
            
                value.hidden = true
            }
            for (i, value) in ets.enumerate() {
                //获取表情按钮
                let button = emoticonButtons[i]
                //设置表情模型
                button.emoticon = value
                //显示按钮
                button.hidden = false
                //判断是否为图片
                if value.type == "0" {
                
                    //设置图片
                    button.setImage(UIImage(named: value.path!), forState: .Normal)
//                    print("-------------\(value.path!)")
                    button.setTitle(nil, forState: .Normal)
                } else {
                
                    //设置 emoji
                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    button.setImage(nil, forState: .Normal)
                }
                
                
                
            }
            
        }
    }
    //赋值
    var indexPath: NSIndexPath? {
    
        didSet{
        
            guard let index = indexPath else {
            
                return
            }
            messageLabel.text = "当前滚动到第\(index.section + 1)组第\(index.item + 1)行"
        }
    }
    //懒加载
    //定义 label
    private lazy var messageLabel: UILabel = {
    
       let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        
        return label
    }()
    //删除按钮
    private lazy var deteleButton: UIButton = {
    
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "deleteButtonAction", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete") , forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_Highlighted"), forState: .Highlighted)
        
        return button
    }()
    //重写
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*!
    *      设置内容
    */
    private func setUpUI() {
        

        //添加表情按钮
        addChildButton()
        //添加控件
        contentView.addSubview(messageLabel)
        contentView.addSubview(deteleButton)
        
        //设置约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
       
    }
    
    private func addChildButton() {
        
        //循环创建
        for _ in 0..<20 {
            let button = WYEmoticonButton()
            //添加点击事件
            button.addTarget(self, action: "emoticonButtonAction:", forControlEvents: .TouchUpInside)
//           button.backgroundColor = RandomColor()
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            //添加在界面
            contentView.addSubview(button)
            //将遍历的 Button 添加到数组中
            emoticonButtons.append(button)
        }
    }
    
    
    @objc private func deleteButtonAction() {
    
        //点击按钮的时候放通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidBUttonDeleteTextViewtextNotification, object: nil)
    }
    
    //按钮点击事件
    
    @objc private func emoticonButtonAction(button: WYEmoticonButton) {
        
        let emotion = button.emoticon!
        print(emotion)
        //  发送点击表情按钮的通知,并且把表情模型传过去
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedEmotionbuttonNotification, object: emotion)
        
        
    }
    //子控件自动布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置宽度
        let itemWith = width / 7
        let itemHeight = height / 3
        
        for (i, button) in emoticonButtons.enumerate() {        
            //列的索引
            let colIndex = i % 7
            //行
            let rowIndex = i / 7
            //设置大小
            button.size = CGSize(width: itemWith, height: itemHeight)
            //设置坐标
            button.x = CGFloat(colIndex) * itemWith
            button.y = CGFloat(rowIndex) * itemHeight
            
        }
        deteleButton.size = CGSize(width: itemWith, height: itemHeight)
        deteleButton.x = width - itemWith
        deteleButton.y = height - itemHeight
    }
}
